import { PERMISSION_ID_ENUM } from '../enums';

// 告知记录本地存储前缀key
const DEVICE_AUTH_RECORD = 'device_authorization_record';

/** 获取所有同意告知弹窗记录 */
const getAllAuthRecord = () => {
  const records = uni.getStorageSync(DEVICE_AUTH_RECORD) || {};
  return records;
};

/**
 * 获取同意告知弹窗记录
 * @param permissionId 权限id
 * @returns boolean 是否同意过告知弹窗 true: 同意过 false: 未同意过
 */
const getAuthNotesRecordByPermissionId = (permissionId: PERMISSION_ID_ENUM | string | string[]): boolean => {
  const records = getAllAuthRecord();
  const permissionIds = Array.isArray(permissionId) ? permissionId : [permissionId];
  return permissionIds.every((id) => !!records[id]);
};

/**
 * 记录同意告知弹窗
 * @param permissionId 权限id
 * @param isAgree 同意结果 true: 同意 false: 不同意
 */
const setAuthNotesRecordByPermissionId = (permissionId: PERMISSION_ID_ENUM | string | string[], isAgree = true) => {
  const records = getAllAuthRecord();
  const permissionIds = Array.isArray(permissionId) ? permissionId : [permissionId];
  permissionIds.forEach((id) => {
    records[id] = isAgree;
  });

  uni.setStorageSync(DEVICE_AUTH_RECORD, records);
};

export { getAuthNotesRecordByPermissionId, setAuthNotesRecordByPermissionId };
